<?php
declare(strict_types=1);

namespace App\ZxAdmin\Sys\Stat;

use App\Controller;

// hook controller_zx_admin_sys_stat_use.php

/**
 * @title   运行概况
 * @auth    true
 * @login   true
 * @menu    true
 * @component   other/stat.vue
 * @icon    el-icon-menu
 * @active  true
 * @affix   false
 * @rank    2
 */
class Stat extends Controller
{
    // hook controller_zx_admin_sys_stat_public_start.php




    // hook controller_zx_admin_sys_stat_public_end.php


    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_sys_stat_index_start.php
        //$stat = \Swoole\Coroutine::stats();


        $time = $this->request->param('time');
        $time = empty($time) ? date('Ymd') : date('Ymd', strtotime($time));
        $_list = read_stat_log($time, 'api');
        empty($_list) && $this->response('0001',[],'数据为空');
        $_list = explode(PHP_EOL, $_list);
        $list = ['api_ms' => [], 'api_m_count' => [], 'api_count' => ['count' => 0, 'ms' => 0],"count"=>[],'worker'=>[],'sys_data'=>[],'sys'=>[]];
        foreach ($_list as $v) {
            $v = trim($v);
            if (empty($v)) {
                continue;
            }

            $d = [];
            [$d['ymd'], $d['route'], $d['count'], $d['ms']] = explode("\t", $v);
            $t = explode(':', $d['ymd']);
            !isset($list['api_ms'][$d['route']]) && $list['api_ms'][$d['route']] = ['count' => 0, 'ms' => 0, 'route' => ''];
            !isset($list['api_m_count'][$t[0]]) && $list['api_m_count'][$t[0]] = ['count' => 0, 'ms' => 0, 'h' => 0];
            $list['api_ms'][$d['route']]['count'] += (int)$d['count'];
            $list['api_ms'][$d['route']]['ms'] += (int)$d['ms'];
            $list['api_ms'][$d['route']]['route'] = str_replace('\\','_', $d['route']);
            $list['api_count']['count'] += (int)$d['count'];
            $list['api_count']['ms'] += (int)$d['ms'];
            $list['api_m_count'][$t[0]]['count'] += (int)$d['count'];
            $list['api_m_count'][$t[0]]['ms'] += (int)$d['ms'];
            $list['api_m_count'][$t[0]]['h'] = $t[0] . ':00:00-' . $t[0] . ':59:59';

        }
        $list['api_ms'] = array_values($list['api_ms']);
        $list['api_m_count'] = array_values($list['api_m_count']);
        foreach ($list['api_ms'] as $k => $v) {
            $list['api_ms'][$k]['ms'] = bcdiv($v['ms'].'', '10000', 5);
            $list['api_ms'][$k]['msp'] = bcdiv($list['api_ms'][$k]['ms'].'', $v['count'].'', 5);
        }

        foreach ($list['api_m_count'] as $k => $v) {
            $list['api_m_count'][$k]['ms'] = bcdiv($v['ms'].'', '10000', 5);
            $list['api_m_count'][$k]['msp'] = bcdiv($list['api_m_count'][$k]['ms'].'', $v['count'].'', 5);
        }

        $_list = read_stat_log($time, 'task');
        $_list = explode(PHP_EOL, $_list.'');
        $_list = array_slice( $_list ,max(0,count($_list)-500),500);
        $list['task_data'] = [];
        foreach ($_list as $v) {
            $v = trim($v);
            if (empty($v)) {
                continue;
            }
            $d = explode("\t", $v);
            if(!isset($d[4])){
                continue;
            }
            [, , $d['tid'], $d['wid'], $d['start'], $d['end'], $d['action']] = $d;
            $d['use'] = bcmul(bcsub($d['end'], $d['start'], 7), '1', 4);
            $d['time']=date('Y-m-d H:i:s',intval($d['start']));
            unset($d['start'],$d['end']);
            $list['task_data'][] = $d;
        }

        $_list = read_stat_log($time, 'workers');
        $_list = explode(PHP_EOL, $_list);
        $list['worker_data'] = [];
        foreach ($_list as $v) {
            $v = trim($v);
            if (empty($v)) {
                continue;
            }
            $d = [];
            [$d['ymd'], $id, $d['request_count'], $d['last_request'], $d['memory']] = explode("\t", $v);
            !isset($list['count'][$d['ymd']]['memory']) && $list['count'][$d['ymd']]['memory'] = 0;
            $list['count'][$d['ymd']]['memory'] += (int)$d['memory'];

            if (empty($list['worker'][$id])) {
                $list['worker'][$id]['type'] = 'line';
                $list['worker'][$id]['smooth'] = 'true';
                $list['worker'][$id]['name'] = sprintf('%04s#', $id);
            }
            $list['worker'][$id]['data'][] = $d['request_count'];
            $list['worker'][$id]['memory'][] = $d['memory'];
            !xn_in_array($d['ymd'], $list['worker_data']) && $list['worker_data'][] = $d['ymd'];
        }
        if (empty($list['worker'][0])) {
            $list['worker'][0]['memory'] = [];
            $list['worker'][0]['data'] = [];
            $list['worker'][0]['type'] = 'line';
            $list['worker'][0]['smooth'] = 'true';
            $list['worker'][0]['name'] = sprintf('%04s#', 0);
        }

        $_list = read_stat_log($time, 'sys');
        $_list = explode(PHP_EOL, $_list);

        $list['sys'][1]['type'] = 'line';
        $list['sys'][1]['yKey'] = 'v';
        $list['sys'][1]['name'] = '01分';
        $list['sys'][2]['type'] = 'line';
        $list['sys'][2]['yKey'] = 'v';
        $list['sys'][2]['name'] = '05分';
        $list['sys'][3]['type'] = 'line';
        $list['sys'][3]['smooth'] = 'true';
        $list['sys'][3]['name'] = '15分';
        foreach ($_list as $v) {
            $v = trim($v);
            if (empty($v)) {
                continue;
            }
            $d = [];
            [$d['ymd'], $d['sys1'], $d['sys2'], $d['sys3']] = explode("\t", $v);

            $list['sys'][1]['data'][] = bcadd($d['sys1'], '0', 2);
            $list['sys'][2]['data'][] = bcadd($d['sys2'], '0', 2);
            $list['sys'][3]['data'][] = bcadd($d['sys3'], '0', 2);
            !xn_in_array($d['ymd'], $list['sys_data']) and $list['sys_data'][] = $d['ymd'];
        }
        empty( $list['sys'][1]['data']) &&  $list['sys'][1]['data']=[];
        empty( $list['sys'][2]['data']) &&  $list['sys'][2]['data']=[];
        empty( $list['sys'][3]['data']) &&  $list['sys'][3]['data']=[];

        $list['count'] = array_values($list['count']);
        $list['worker'] = array_values($list['worker']);
        $list['sys'] = array_values($list['sys']);
        // hook controller_zx_admin_sys_stat_index_end.php
        $this->response('0000', $list);

    }
    

    // hook controller_zx_admin_sys_stat_end.php
}

?>